Python Logging
Table of Content
Python Logging#
Python has a built-in logging module that provides a simple and flexible way to log events.
The logging module provides four main components:
- Loggers: Loggers are responsible for generating log records. They are organized in a hierarchical namespace, with the root logger at the top.
- Handlers: Handlers send log records to a destination, such as a file, a database, or the console.
- Formatters: Formatters format log records before they are sent to a handler. They can be used to control the appearance of the log records.
- Filters: Filters can be used to selectively control which log records are sent to a handler.
Other Logging examples#
using json to create logging config#
logging.json
{
"version": 1,
"disable_existing_loggers": false,
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "simple"
}
},
"formatters": {
"simple": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
}
},
"loggers": {
"root": {
"handlers": ["console"],
"level": "DEBUG"
}
}
}
package/__init__.py
import logging.config
import pathlib
import json
config_file = pathlib.Path(__file__).parents[0].joinpath("logging.json")
with open(config_file, "r", encoding="utf-8") as f:
logging_config = json.load(f)
logging.config.dictConfig(logging_config)
demo.py
logger = logging.getLogger(__name__)
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
Add console color#
pip install coloredlogs
add console color
import logging.config
import pathlib
import json
import coloredlogs
config_file = pathlib.Path(__file__).parents[0].joinpath("logging.json")
with open(config_file, "r", encoding="utf-8") as f:
logging_config = json.load(f)
logging.config.dictConfig(logging_config)
coloredlogs.install(level='DEBUG')
